package com.example.administrator.capital.login;

import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.capital.R;
import com.example.administrator.capital.base.BaseActivity;
import com.example.administrator.capital.presenter.LoginPresenter;
import com.example.administrator.capital.presenter.base.IViewRequest;
import com.example.administrator.capital.utils.CommonUtils;
import com.example.administrator.capital.widget.CountDownTimerUtils;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ForgetActivity extends BaseActivity {

    @BindView(R.id.phoneNum)
    EditText mPhone;
    @BindView(R.id.get_pic_code)
    TextView mGetPicCode;
    @BindView(R.id.edit_pic_code)
    EditText mEditPicCode;
    @BindView(R.id.get_code)
    TextView mGetCode;
    @BindView(R.id.edit_code)
    EditText mEditCode;
    @BindView(R.id.eidt_password)
    EditText mEidtPassword;
    @BindView(R.id.btn_eye)
    CheckBox mBtnEye;
    @BindView(R.id.eidt_password_sure)
    EditText mEidtPasswordSure;
    @BindView(R.id.btn_eye_1)
    CheckBox mBtnEye1;
    @BindView(R.id.registerInBtn)
    Button mRegisterInBtn;
    private String phone;
    private TextView mBack;
    private LoginPresenter mLoginPresenter;
    @Override
    public int getLayoutId() {
        return R.layout.activity_forget;
    }

    @Override
    public void initView() {
    isPasswordShowListener();
        mBack = findViewById(R.id.app_title_back);
        mLoginPresenter = new LoginPresenter(null);


    }

    @Override
    public void setListener() {
        mRegisterInBtn.setOnClickListener(this);
        mGetCode.setOnClickListener(this);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void setData() {

    }

    private void getYZM( String mobile) {
        mLoginPresenter.getYZM(new IViewRequest<String>() {
            @Override
            public void onFailMsg(String var) {

            }

            @Override
            public void onCode(int code) {

            }

            @Override
            public void onSuccess(Response<String> data) {
                CountDownTimerUtils utils = new CountDownTimerUtils(mGetCode, 60000, 1000);
                utils.start();
            }

            @Override
            public void onFail(Response<String> error) {

            }
        },mobile);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.get_code:
                if (isPhoneNumber()) {
                    getYZM(phone);

                }
                break;
            case R.id.registerInBtn:
                showActivity(LoginActivity.class,null);
                break;
        }
    }

    /**
     * @return
     * 判断手机号
     */
    private boolean isPhoneNumber() {
        phone = mPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(mContext, "手机号不能为空!", Toast.LENGTH_SHORT).show();
            return false;
        } else if (CommonUtils.isMobileNO(phone)) {
            return true;
        } else {
            Toast.makeText(mContext, "手机号不合法!", Toast.LENGTH_SHORT).show();
            return false;
        }
    }
    /*显示密码*/
    private void isPasswordShowListener() {
        mBtnEye.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int passwordLength = mEidtPassword.getText().length();
                mEidtPassword.setInputType(isChecked ?
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) :
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
                mEidtPassword.setSelection(passwordLength);
            }
        });
        mBtnEye1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int passwordLength = mEidtPasswordSure.getText().length();
                mEidtPasswordSure.setInputType(isChecked ?
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) :
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
                mEidtPasswordSure.setSelection(passwordLength);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
